package com.insea.webapp.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.insea.model.Acte;
import com.insea.model.Examen;
import com.insea.model.Patient;
import com.insea.model.User;


public class ExamenAction extends BaseAction{

	 private List examens;
	 private List patients;
	 private List<Acte> actes;
	 private String query;
	 private String id;
	 private Patient patient;
	 private Examen examen;
	 private User user;
	 private String[] select;
	 private List<Acte> selectedActes;
  
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public List getExamens() {
        return examens;
    }
	
	  public List getPatients() {
			return patients;
		}

		public List getActes() {
			return actes;
		}
		
		public Patient getPatient() {
			return patient;
		}

		public void setPatient(Patient patient) {
			this.patient = patient;
		}

		public Examen getExamen() {
			return examen;
		}

		public void setExamen(Examen examen) {
			this.examen = examen;
		}

		public User getUser() {
			return user;
		}

		public void setUser(User user) {
			this.user = user;
		}

    
    
    public String listPatient() {
    	patients = patientManager.getAll();
        return SUCCESS;
    }
    public String listActe() {
    	actes = acteManager.getAll();
        return SUCCESS;
    }
    
  
    public String list() {
		if(query == null || "".equals(query)){
			patients = patientManager.getAll();	
		}else{
			patients = patientManager.getPatientsByName(query);
		}
		return SUCCESS;
	}
    
	

	public String save() throws Exception {
		
		
		patient = (Patient) getRequest().getSession().getAttribute("patient");
		log.debug("patient is :" +patient);
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		user=(User) auth.getPrincipal();
		log.debug("user is :" +user.getFullName());
		log.debug("num exa is :" +examen);

		Date date = new Date();
		examen.setDateDebutExamen(date);
		examen.setUser(user);
		examen.setPatient(patient);
		selectedActes = (List<Acte>) getRequest().getSession().getAttribute("actes");
		examen.addActes(selectedActes);
		
	    examen = examenManager.save(examen);
	    getRequest().getSession().removeAttribute("actes");
	    String key = "examen.added";
	    saveMessage(getText(key));

	        return SUCCESS;
	}
	    public String delete() {
	        examenManager.remove(examen.getId());
	        saveMessage(getText("examen.deleted"));

	        return SUCCESS;
	    }
	    
		public String listActes() {
			if(query == null || "".equals(query)){
				actes = acteManager.getAll();	
			}else{
				actes = acteManager.getActesByName(query);
			}
			if (id != null) {
		        patient = patientManager.get(Long.valueOf(id));
		        getRequest().getSession().setAttribute("patient", patient);
		        log.debug("patient is :" +patient);
		    } else {
		        patient = new Patient();
		    }
			return SUCCESS;
		}
		
		public String listExamens() {
		    if (id != null) {
		        patient = patientManager.get(Long.valueOf(id));
		    } else {
		        patient =  (Patient) getRequest().getSession().getAttribute("patient");
		    }
		   // examens = examenManager.getListExamensByPatient(patient);
		    
		    if(query == null || "".equals(query)){
				examens = examenManager.getListExamensByPatient(patient);	
			}else{
				examens = examenManager.getExamensByName(query, patient);
			}
		    getRequest().getSession().setAttribute("patient", patient);
		    return SUCCESS;
		}
		public String detailExamen() {
		    if (id != null) {
		        examen = examenManager.get(Long.valueOf(id));
		    } else {
		    	examen = new Examen();
		    }
		    actes = examen.getListActe();
		    return SUCCESS;
		}
		
		public String AddActe(){
			actes = acteManager.getAll();
			selectedActes = (List<Acte>) getRequest().getSession().getAttribute("actes");
			if(selectedActes == null){
				selectedActes = new ArrayList<Acte>();
			}
			for (String s : select){
				log.debug("xxxxxxxxx " + s);
				Acte tmp = acteManager.get(Long.valueOf(s));
				if(!(selectedActes.contains(tmp))){
					selectedActes.add(tmp);					
				}
			}
			getRequest().getSession().setAttribute("actes", selectedActes);
			return SUCCESS;
		}

		public String[] getSelect() {
			return select;
		}

		public void setSelect(String[] select) {
			this.select = select;
		}
		
		public String listExamen(){
			  patient = patientManager.get(Long.valueOf(id));
			
			if(query == null || "".equals(query)){
				examens = examenManager.getListExamensByPatient(patient);	
			}else{
				examens = examenManager.getExamensByName(query, patient);
			}
			
			return SUCCESS;
		}
}

	
